package mini.reanimate;

import java.awt.Point;
import java.util.ArrayList;

import owg.util.opengl.ColorF;
import bogus.core.BogusCore;

public class Letter{
	private static final float DOT_SCALE = .35f, OUT_SCALE = .42f;
	
	public static final Letter B = new Letter(new Point(7, 12), new Point(6, 16), new Point(7, 21), new Point(7, 25), new Point(8, 29), new Point(8, 33), new Point(8, 37), new Point(8, 41), new Point(8, 45), new Point(9, 50), new Point(8, 55), new Point(8, 60), new Point(7, 65), new Point(7, 69), new Point(6, 74), new Point(7, 78), new Point(6, 83), new Point(6, 87), new Point(5, 93), new Point(4, 102), new Point(3, 106), new Point(3, 110), new Point(3, 116), new Point(3, 121), new Point(3, 125), new Point(3, 129), new Point(3, 134), new Point(3, 139), new Point(3, 143), new Point(3, 147), new Point(4, 152), new Point(5, 156), new Point(7, 161), new Point(6, 166), new Point(7, 170), new Point(6, 175), new Point(7, 179), new Point(7, 183), new Point(7, 187), new Point(11, 189), new Point(9, 193), new Point(8, 197), new Point(8, 201), new Point(7, 206), new Point(8, 210), new Point(8, 214), new Point(9, 219), new Point(14, 222), new Point(18, 222), new Point(22, 220), new Point(26, 219), new Point(30, 218), new Point(34, 216), new Point(39, 213), new Point(44, 210), new Point(48, 208), new Point(53, 205), new Point(57, 200), new Point(61, 195), new Point(63, 190), new Point(67, 185), new Point(70, 180), new Point(71, 175), new Point(72, 170), new Point(75, 164), new Point(74, 159), new Point(75, 154), new Point(75, 149), new Point(75, 144), new Point(73, 138), new Point(71, 132), new Point(69, 127), new Point(67, 122), new Point(62, 119), new Point(57, 115), new Point(54, 110), new Point(48, 107), new Point(43, 103), new Point(38, 101), new Point(33, 99), new Point(7, 97), new Point(11, 96), new Point(16, 96), new Point(22, 95), new Point(26, 94), new Point(32, 93), new Point(36, 91), new Point(42, 90), new Point(46, 88), new Point(50, 84), new Point(54, 83), new Point(58, 80), new Point(60, 75), new Point(63, 70), new Point(65, 64), new Point(66, 59), new Point(66, 53), new Point(67, 48), new Point(66, 41), new Point(65, 36), new Point(63, 30), new Point(62, 25), new Point(57, 19), new Point(54, 13), new Point(51, 8), new Point(45, 4), new Point(40, 3), new Point(35, 2), new Point(30, 2), new Point(24, 3), new Point(19, 4), new Point(13, 5), new Point(8, 5)),
	O = new Letter(new Point(49, 220), new Point(43, 224), new Point(38, 225), new Point(33, 224), new Point(28, 223), new Point(23, 221), new Point(18, 219), new Point(14, 214), new Point(13, 208), new Point(11, 202), new Point(10, 196), new Point(9, 190), new Point(8, 185), new Point(7, 180), new Point(7, 175), new Point(6, 170), new Point(6, 164), new Point(7, 158), new Point(6, 153), new Point(5, 148), new Point(6, 143), new Point(6, 137), new Point(6, 131), new Point(6, 125), new Point(7, 120), new Point(7, 114), new Point(8, 108), new Point(8, 103), new Point(8, 98), new Point(8, 93), new Point(9, 88), new Point(9, 83), new Point(10, 78), new Point(11, 73), new Point(13, 67), new Point(13, 62), new Point(14, 57), new Point(15, 52), new Point(17, 47), new Point(20, 41), new Point(23, 36), new Point(27, 31), new Point(31, 27), new Point(35, 23), new Point(39, 21), new Point(43, 19), new Point(47, 17), new Point(51, 14), new Point(55, 13), new Point(59, 12), new Point(63, 13), new Point(66, 17), new Point(70, 20), new Point(73, 24), new Point(75, 28), new Point(77, 32), new Point(79, 36), new Point(82, 43), new Point(82, 48), new Point(82, 52), new Point(82, 56), new Point(82, 60), new Point(82, 64), new Point(83, 69), new Point(82, 73), new Point(82, 77), new Point(83, 81), new Point(82, 86), new Point(83, 90), new Point(84, 95), new Point(85, 99), new Point(85, 104), new Point(85, 108), new Point(86, 112), new Point(86, 116), new Point(86, 120), new Point(86, 124), new Point(86, 128), new Point(86, 132), new Point(86, 136), new Point(86, 140), new Point(86, 144), new Point(86, 148), new Point(87, 152), new Point(85, 156), new Point(85, 160), new Point(85, 164), new Point(84, 169), new Point(84, 173), new Point(84, 177), new Point(84, 181), new Point(83, 185), new Point(83, 189), new Point(82, 193), new Point(81, 197), new Point(79, 201), new Point(77, 205), new Point(75, 209), new Point(71, 213), new Point(66, 217), new Point(61, 220), new Point(55, 221)),
	R = new Letter(new Point(7, 221), new Point(7, 215), new Point(8, 210), new Point(8, 205), new Point(10, 199), new Point(10, 194), new Point(10, 189), new Point(11, 184), new Point(11, 179), new Point(11, 174), new Point(12, 168), new Point(13, 162), new Point(13, 156), new Point(12, 151), new Point(13, 145), new Point(13, 140), new Point(13, 135), new Point(12, 129), new Point(12, 124), new Point(12, 118), new Point(11, 113), new Point(12, 108), new Point(12, 103), new Point(11, 98), new Point(12, 91), new Point(12, 85), new Point(13, 79), new Point(13, 74), new Point(12, 67), new Point(12, 62), new Point(11, 56), new Point(11, 51), new Point(11, 45), new Point(11, 40), new Point(11, 34), new Point(13, 29), new Point(12, 23), new Point(12, 18), new Point(11, 13), new Point(16, 11), new Point(21, 9), new Point(26, 7), new Point(30, 7), new Point(34, 8), new Point(38, 9), new Point(44, 10), new Point(48, 11), new Point(52, 14), new Point(56, 17), new Point(60, 19), new Point(64, 21), new Point(67, 25), new Point(70, 29), new Point(74, 33), new Point(75, 37), new Point(77, 42), new Point(77, 47), new Point(76, 51), new Point(74, 55), new Point(74, 59), new Point(73, 64), new Point(71, 68), new Point(69, 72), new Point(65, 76), new Point(62, 80), new Point(57, 84), new Point(52, 86), new Point(47, 89), new Point(42, 91), new Point(36, 93), new Point(31, 94), new Point(26, 94), new Point(21, 94), new Point(16, 94), new Point(14, 94), new Point(17, 98), new Point(22, 102), new Point(26, 107), new Point(30, 111), new Point(34, 114), new Point(35, 118), new Point(38, 122), new Point(40, 126), new Point(43, 131), new Point(45, 135), new Point(47, 139), new Point(49, 143), new Point(50, 147), new Point(51, 151), new Point(53, 155), new Point(54, 160), new Point(57, 164), new Point(57, 168), new Point(58, 172), new Point(59, 176), new Point(60, 180), new Point(62, 184), new Point(65, 188), new Point(67, 192), new Point(68, 196), new Point(69, 200), new Point(70, 204), new Point(71, 208), new Point(72, 212), new Point(73, 216)),
	G = new Letter(new Point(81, 94), new Point(82, 88), new Point(83, 83), new Point(83, 77), new Point(83, 72), new Point(82, 66), new Point(81, 60), new Point(82, 54), new Point(82, 48), new Point(81, 43), new Point(79, 38), new Point(78, 33), new Point(74, 28), new Point(70, 23), new Point(64, 18), new Point(59, 16), new Point(53, 14), new Point(47, 14), new Point(42, 14), new Point(37, 17), new Point(32, 21), new Point(28, 25), new Point(25, 29), new Point(22, 33), new Point(19, 37), new Point(17, 42), new Point(15, 47), new Point(14, 51), new Point(13, 56), new Point(12, 61), new Point(13, 65), new Point(11, 70), new Point(11, 75), new Point(10, 79), new Point(10, 83), new Point(9, 89), new Point(9, 94), new Point(10, 99), new Point(9, 103), new Point(9, 107), new Point(9, 112), new Point(8, 116), new Point(8, 120), new Point(7, 125), new Point(8, 129), new Point(9, 134), new Point(9, 138), new Point(9, 142), new Point(10, 147), new Point(10, 152), new Point(11, 156), new Point(11, 160), new Point(11, 164), new Point(11, 169), new Point(11, 173), new Point(12, 177), new Point(12, 181), new Point(13, 185), new Point(13, 190), new Point(15, 194), new Point(16, 198), new Point(19, 202), new Point(21, 206), new Point(23, 210), new Point(27, 214), new Point(31, 215), new Point(35, 216), new Point(39, 218), new Point(44, 217), new Point(48, 216), new Point(52, 216), new Point(56, 215), new Point(60, 210), new Point(64, 207), new Point(66, 202), new Point(69, 197), new Point(73, 192), new Point(75, 187), new Point(77, 182), new Point(79, 176), new Point(80, 171), new Point(81, 166), new Point(82, 160), new Point(82, 154), new Point(82, 149), new Point(81, 144), new Point(82, 139), new Point(82, 134), new Point(82, 129), new Point(82, 124), new Point(76, 123), new Point(71, 122), new Point(66, 123), new Point(61, 124), new Point(56, 124), new Point(51, 125), new Point(46, 128), new Point(41, 129), new Point(36, 130)),
	U = new Letter(new Point(11, 4), new Point(10, 9), new Point(11, 13), new Point(10, 18), new Point(9, 23), new Point(9, 28), new Point(9, 34), new Point(9, 38), new Point(9, 43), new Point(8, 47), new Point(7, 51), new Point(7, 55), new Point(7, 59), new Point(7, 64), new Point(7, 69), new Point(7, 73), new Point(6, 78), new Point(7, 83), new Point(8, 87), new Point(8, 93), new Point(8, 98), new Point(8, 102), new Point(8, 108), new Point(8, 113), new Point(8, 118), new Point(8, 123), new Point(9, 127), new Point(9, 132), new Point(8, 136), new Point(9, 140), new Point(9, 146), new Point(9, 150), new Point(9, 154), new Point(10, 159), new Point(10, 164), new Point(11, 169), new Point(12, 173), new Point(12, 177), new Point(12, 182), new Point(13, 187), new Point(13, 191), new Point(15, 195), new Point(16, 199), new Point(16, 203), new Point(19, 207), new Point(22, 211), new Point(26, 213), new Point(30, 216), new Point(34, 217), new Point(38, 217), new Point(42, 217), new Point(46, 216), new Point(50, 213), new Point(54, 208), new Point(57, 203), new Point(58, 198), new Point(60, 193), new Point(62, 187), new Point(64, 181), new Point(66, 176), new Point(67, 171), new Point(69, 165), new Point(70, 160), new Point(71, 155), new Point(71, 150), new Point(72, 145), new Point(73, 140), new Point(74, 135), new Point(75, 130), new Point(76, 123), new Point(76, 117), new Point(77, 112), new Point(78, 106), new Point(79, 101), new Point(78, 96), new Point(80, 90), new Point(81, 84), new Point(81, 79), new Point(81, 74), new Point(81, 69), new Point(81, 63), new Point(81, 58), new Point(81, 53), new Point(81, 48), new Point(80, 43), new Point(81, 37), new Point(82, 31), new Point(82, 24), new Point(82, 19), new Point(82, 13)),
	S = new Letter(new Point(80, 72), new Point(81, 66), new Point(81, 61), new Point(80, 56), new Point(80, 50), new Point(79, 45), new Point(79, 40), new Point(78, 35), new Point(75, 30), new Point(71, 25), new Point(65, 21), new Point(60, 19), new Point(55, 17), new Point(50, 14), new Point(45, 15), new Point(40, 17), new Point(36, 21), new Point(32, 25), new Point(28, 29), new Point(26, 33), new Point(23, 37), new Point(20, 42), new Point(18, 46), new Point(18, 50), new Point(16, 55), new Point(16, 59), new Point(16, 63), new Point(16, 68), new Point(17, 73), new Point(18, 78), new Point(20, 82), new Point(24, 86), new Point(28, 89), new Point(31, 93), new Point(35, 95), new Point(39, 98), new Point(43, 102), new Point(47, 105), new Point(51, 109), new Point(53, 113), new Point(54, 117), new Point(56, 121), new Point(59, 125), new Point(61, 129), new Point(63, 133), new Point(65, 137), new Point(66, 141), new Point(69, 145), new Point(70, 149), new Point(71, 153), new Point(74, 157), new Point(76, 162), new Point(77, 166), new Point(77, 170), new Point(79, 174), new Point(78, 178), new Point(78, 182), new Point(78, 188), new Point(77, 192), new Point(75, 196), new Point(74, 200), new Point(73, 204), new Point(69, 208), new Point(66, 212), new Point(62, 216), new Point(57, 219), new Point(52, 222), new Point(47, 222), new Point(42, 222), new Point(37, 222), new Point(32, 221), new Point(27, 220), new Point(22, 219), new Point(17, 214), new Point(14, 208), new Point(12, 203), new Point(12, 198), new Point(12, 193), new Point(11, 188), new Point(12, 183));
	
	private Point[] points;
	
	public Letter(Point... p){
		points = smoothPointArray(smoothPointArray(p));
	}
	
	private Point[] smoothPointArray(Point[] array){
		ArrayList<Point> list = new ArrayList<Point>();
		for (int i = 0; i < array.length; i++){
			if (i == array.length - 1)
				list.add(array[i]);
			else{
				Point p = array[i], q = array[i + 1];
				list.add(new Point((p.x + q.x) / 2, (p.y + q.y) / 2));
			}
		}
		return list.toArray(new Point[]{});
	}
	
	/**retrieve which point index is at the specified "percentage" of completion*/
	private int getIndexByPercent(double percent){
		return (int)Math.min(points.length * percent, points.length - 1) - 1;
	}
	
	/**retrieve the point at the specified "percentage" of completion*/
	public Point getPointByPercent(double percent){
		int index = getIndexByPercent(percent);
		if (index >= 0)
			return points[index];
		return null;
	}
	
	/**render the letter at the given location, scale, and percentage*/
	public void render(double x, double y, float xscale, float yscale, float percentage){
		BogusCore.egl.setColor(ColorF.WHITE);
		
		for (int i = 0; i <= getIndexByPercent(percentage); i++){
			Point p = points[i];
			Reanimate.sand.render(0, x + (p.x * xscale), y + (p.y * yscale), OUT_SCALE, OUT_SCALE, 0);
		}
		
		for (int i = 0; i <= getIndexByPercent(percentage); i++){
			Point p = points[i];
			Reanimate.sand.render(1, x + (p.x * xscale), y + (p.y * yscale), DOT_SCALE, DOT_SCALE, 0);
		}
	}

}
